﻿using UnityEngine;
using System;
using System.Collections.Generic;
using System.Collections;


public class WorldSceneManager_GStar : LTEventListenerC {
	public static int newOpenAreaId = 0;
	public static bool isSentSearch = false;
	public static bool isSuccessSearch = false;
	public static int sentResearchAreaId = 0;
	public static bool isRegionLevelUp = false;

	public GameObject worldMapObject;
	public GameObject areaViewObject;
	public GameObject dispatchInfoViewObject;

	public UILabel[] areaLevelInWorldMap;
	public UILabel[] areaDispatchMemberCountInWorldMap;
	public UILabel[] areaDispatchRemainTimeInWorldMap;
	public GameObject[] areaDispatchIngMarkObject;
	public GameObject[] areaNoDispatchMarkObject;
	public GameObject[] areaNotOpenTextObject;
    public GameObject[] betweenAreaRoadObjectInWorldMap;

	public UISprite[] areaIconImage;
	public UILabel[] areaNameLabel;
	public UILabel areaLevelLabel;
	public UILabel areaExpLabel;
	public UILabel areaOpenCountLabel;
	public UILabel areaMemberCurrCountLabel;
	public UILabel areaMemberMaxCountLabel;
	public UILabel areaRemainTimeLabel;
	public GameObject[] areaBonusSpecObject;
	public UISlider areaExpBar;

	public UISprite[] notOpenAreaImage;

	GameObject[] tipObject;
	UILabel[] tipBodyLabel;

	public GameObject[] areaIngIconList;
	public GameObject[] areaDisPatchIconList;

	public GameObject newAreaIcon;

	public GameObject wolrdMapBackground;
	public GameObject[] wolrdMapBackground2;
	public GameObject[] areaBackground;

	public GameObject[] areaPointObjectList;

	public GameObject researchButton;
	public GameObject dispatchButton;

	public GameObject dispatchNormalButton;
	public GameObject dispatchIngButton;
	public GameObject dispatchEndButton;

	public GameObject selectDispatchMemberListObject;

	public GameObject[] dispatchDetailState;
	public UILabel[] dispatchMemberCountInDetail;
	public UILabel[] dispatchMemberMaxCountInDetail;
	public UILabel[] dispatchTimeInDetail;
	public GameObject[] dispatchButtonsInDetail;

	public GameObject expansionWarButton;

    public Camera BGCamera;
    public Camera UICamera;

	const int RareMax = 6;
	const float StarCenter = 10.0f;
	const float StarGap = 26.0f;

	public override void AwakeScene ()
	{
		base.AwakeScene ();

		hasLeftTopMenu = true;
		hasBackButton = true;
		//SetActiveHelpButton(true);

		WorldExtensionBattleStageScene.isExpansionWar = false;

		//BGM 재생 임시 적용
		if( UserData.Instance.userModel.regions.regionMap_Dic.Count == 1 )
			BGM = "BGM_Map_Poeta";
        else if (UserData.Instance.userModel.regions.regionMap_Dic.Count == 2)
			BGM = "BGM_Map_Berteron";
        else if (UserData.Instance.userModel.regions.regionMap_Dic.Count == 3)
            BGM = "BGM_Map_Teobomos";
        else if (UserData.Instance.userModel.regions.regionMap_Dic.Count == 4)
            BGM = "BGM_Map_Eltenen";
	}

	// Use this for initialization
	public override void StartScene () {
        SetActiveHelpButton(false);
		if( UserData.Instance.userModel == null )
		{
			ResourceManager.Inst.PatchDataFile( ()=> {
				GameData.Init();
				CashData.Init();
				//			TextData.Init();
				EventData.Init();
			});

			NetworkManager.Instance.C2S_LOGIN(AuthPlatformID.None, "shinpc00", string.Empty, ResponseLogin, null);
		}
		else
		{
			SetWorldMapInfos();
		}

		if( newOpenAreaId != 0 )
		{
			Popup_OneButton po = PopupManager.PushPopup(PopupKind.OneButton) as Popup_OneButton;
			po.SetPopupWithTitleAndBody(TextController.GetStringWithId("uiPopupExpansionCaution1"), string.Format("{0}\n\n{1}", TextController.GetStringWithId("uiPopupExpansionCaution2").Replace("{0}", TextController.GetStringWithId(string.Format("region{0}", newOpenAreaId.ToString()))), TextController.GetStringWithId("uiPopupExpansionCaution3")));
			newOpenAreaId = 0;
		}
	}

	private void ResponseLogin(ResponseModel<UserModel> result)
	{
		if (result.ecode != 0)
			return;

		UserData.Instance.userModel = result.data;

		SetWorldMapInfos();
	}

    public override void BackButton()
    {
        base.BackButton();

        SceneController.LoadLevel("Main");
    }
	
	// Update is called once per frame
	void Update () {
	}

	int selectedMemberIndex = -1;
	public override void OnEvent (int eventID)
	{
		base.OnEvent (eventID);

		if( eventID < 100 )
		{
            if( !UserData.Instance.isExistRaidBoss )
                CreateBossAndGoToBossListScene(eventID+100);
            else
            {
                sentResearchAreaId = eventID + 100;
                SceneController.LoadLevel("BossList");
            }

		}
	}

    void CreateBossAndGoToBossListScene(int regionId)
    {
        NetworkManager.Instance.C2S_GSTAR_ADD_RAIDBOSS(regionId, (responseData =>
            {
                sentResearchAreaId = regionId;
                SceneController.LoadLevel("BossList");
            }
        ));
    }

	void SetWorldMapInfos()
	{
		foreach(KeyValuePair<string, Region> pair in UserData.Instance.userModel.regions.regionMap_Dic)
		{
			areaLevelInWorldMap[pair.Value.id-100].text = string.Format("{0}", TextController.GetStringWithId(string.Format("region{0}", pair.Value.id)));
			areaLevelInWorldMap[pair.Value.id-100].color = Color.white;
            areaLevelInWorldMap[pair.Value.id - 100].alpha = 1f;
			areaDispatchIngMarkObject[pair.Value.id-100].SetActive(false);
			areaNoDispatchMarkObject[pair.Value.id-100].SetActive(true);
//			areaNotOpenTextObject[pair.Value.id-100].SetActive(false);
			areaNoDispatchMarkObject[pair.Value.id-100].transform.parent.gameObject.GetComponent<LTButtonMessage>().enabled = true;
			if( areaNoDispatchMarkObject[pair.Value.id-100].transform.FindChild("world_icon_open") != null )
				areaNoDispatchMarkObject[pair.Value.id-100].transform.FindChild("world_icon_open").gameObject.SetActive(true);
            betweenAreaRoadObjectInWorldMap[pair.Value.id - 100].SetActive(true);
		}

		if( UserData.Instance.userModel.regions.regionMap_Dic.Count < 4 )
		{
			expansionWarButton.transform.parent = areaNoDispatchMarkObject[UserData.Instance.userModel.regions.regionMap_Dic.Count].transform.parent;
			expansionWarButton.transform.localPosition = new Vector3(0f, 45f, 0f);
			expansionWarButton.transform.localScale = Vector3.one;
			expansionWarButton.GetComponent<LTButtonMessage>().m_MethodDeliver[0].m_EventId = UserData.Instance.userModel.regions.regionMap_Dic.Count;
		}
		else
		{
			expansionWarButton.SetActive(false);
		}

        //areaLevelInWorldMap[UserData.Instance.userModel.regions.regionMap_Dic.Count].color = new Color(255/255f, 112/255f, 112/255f);
        if (UserData.Instance.userModel.regions.regionMap_Dic.Count > 0)
        {
            betweenAreaRoadObjectInWorldMap[UserData.Instance.userModel.regions.regionMap_Dic.Count - 1].SetActive(true);
            betweenAreaRoadObjectInWorldMap[UserData.Instance.userModel.regions.regionMap_Dic.Count - 1].transform.FindChild("on").gameObject.SetActive(true);
            betweenAreaRoadObjectInWorldMap[UserData.Instance.userModel.regions.regionMap_Dic.Count - 1].transform.FindChild("off").gameObject.SetActive(false);
        }
        areaLevelInWorldMap[UserData.Instance.userModel.regions.regionMap_Dic.Count].color = Color.white;
        areaLevelInWorldMap[UserData.Instance.userModel.regions.regionMap_Dic.Count].alpha = 1f;
        wolrdMapBackground2[UserData.Instance.userModel.regions.regionMap_Dic.Count].SetActive(true);
	}

    void GetQuestInfo()
    {
        NetworkManager.Instance.C2S_GET_QUESTINFO(QuestManager.instance.ResponseGetQuestInfo);
    }
}
